//
//  QDOriginTitleTableViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/1.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 原始的类型
enum OriginType: String {
    /// app常见问题
    case questionForAPP
    /// 乘车常见问题
    case questionForMetro
    /// 出行指南
    case guide = "出行指南"
    /// 安全须知
    case safe = "安全须知"
    /// 应急指引
    case safeGuide = "应急指引"
}





extension OriginType {
    
    /// 所对应的cellClass
    var cellClass: AnyClass {
        return QDOriginalTitleTableCell.self
    }

    /// cell高度
    var cellHeight: CGFloat {
        return 58
    }
    
    /// 请求的url
    var requestUrl: String {
        switch self {
        case .questionForAPP,.questionForMetro: return QDStaticHostUrl + "/document/faq"
        case .guide, .safe, .safeGuide: return QDStaticHostUrl + "/document/convenience"
        }
    }
    
    /// 参数类型
    var type: String {
        switch self {
        case .questionForMetro: return "2"
        case .questionForAPP: return "1"
        case .guide: return "28"
        case .safe: return "43"
        case .safeGuide: return "42"
        }
    }
    
    /// 网页的标题
    var webTitle: String {
        switch self {
        case .questionForAPP,.questionForMetro: return ""
        case .guide,.safe,.safeGuide: return self.rawValue
        }
    }
}





/// 用于适配原有界面的控制器样式，多用于文章列表，类型查看 OriginType
final class QDOriginTitleTableViewController: NWTableViewController {

    /// 类型，默认为app常见问题
    var type = OriginType.questionForAPP
    
    /// 数据
    private var textLists = [QDTextListModel]()
    
    convenience init(type: OriginType = .questionForAPP) {
        self.init()
        self.type = type
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.register(type.cellClass, forCellReuseIdentifier: "cell")
        tableView.rowHeight = type.cellHeight
        
        if type == .guide {//出行指南设置title
            navigationItem.title = type.rawValue
        }
        
        headerRefreshBeginHandler()
    }
    
    override func headerRefreshBeginHandler() {
        currentPage = 0
        tableView.mj_footer.resetNoMoreData()
        requestData()
    }
    
    override func footerRefreshBeginHandler() {
        requestData()
    }
}







extension QDOriginTitleTableViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return textLists.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        if let titleCell = cell as? QDOriginalTitleTableCell {
            let textModel = textLists[indexPath.row]
            
            titleCell.titleLabel.text = textModel.title
        }
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let textModel = textLists[indexPath.row]
        
//        pushToShareWebViewController(url: textModel.webUrl, shareTitle: textModel.title)
        pushToShareWebViewController(url: textModel.webUrl, shareTitle: textModel.title, shareDesc: textModel.author)
    }
}







extension QDOriginTitleTableViewController {
    
    /// 请求数据
    private func requestData(){
        
        QDNetworkingManager.request(url: type.requestUrl, isArray: true, paramteres: ["type": type.type,"size":10,"page": currentPage], arraySuccess: { (msg, datas) in
            
            self.endHeaderRefreshing()
            self.endFooterRefreshing()
            
            guard let textDatas = NSArray.yy_modelArray(with: QDTextListModel.self, json: datas) else {
                self.view.showTextHUD(text: "数据错误!"); return
            }
            
            if self.currentPage == 0 {
                self.textLists.removeAll()
            }
            
            if textDatas.count < 10 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            self.currentPage += 1
            self.textLists += (textDatas as? [QDTextListModel] ?? [QDTextListModel]())
            self.tableView.reloadData()
            
        }) { (msg, errorType) in
            
            self.endHeaderRefreshing()
            self.endFooterRefreshing()
            self.view.showTextHUD(text: msg)
        }
    }
}



